<!DOCTYPE html>




<html class="theme-next gemini" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="测试,Java测试,Mockito," />





  <link rel="alternate" href="/atom.xml" title="流水不腐小夏" type="application/atom+xml" />






<meta name="description" content="Mockito浅谈 [TOC] Mockito简介 什么是mock？在软件开发的世界之外, “mock”一词是指模仿或者效仿。 因此可以将“mock”理解为一个替身，替代者. 在软件开发中提及”mock”，通常理解为模拟对象或者Fake。  为什么需要Mock?Mock是为了解决units之间由于耦合而难于被测试的问题。所以mock object是unit test的一部分。   Mock的好处是">
<meta name="keywords" content="测试,Java测试,Mockito">
<meta property="og:type" content="article">
<meta property="og:title" content="Mockito浅谈">
<meta property="og:url" content="http://mycommons.cn/2016/07/19/Mockito/index.html">
<meta property="og:site_name" content="流水不腐小夏">
<meta property="og:description" content="Mockito浅谈 [TOC] Mockito简介 什么是mock？在软件开发的世界之外, “mock”一词是指模仿或者效仿。 因此可以将“mock”理解为一个替身，替代者. 在软件开发中提及”mock”，通常理解为模拟对象或者Fake。  为什么需要Mock?Mock是为了解决units之间由于耦合而难于被测试的问题。所以mock object是unit test的一部分。   Mock的好处是">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2016-07-19T06:15:55.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Mockito浅谈">
<meta name="twitter:description" content="Mockito浅谈 [TOC] Mockito简介 什么是mock？在软件开发的世界之外, “mock”一词是指模仿或者效仿。 因此可以将“mock”理解为一个替身，替代者. 在软件开发中提及”mock”，通常理解为模拟对象或者Fake。  为什么需要Mock?Mock是为了解决units之间由于耦合而难于被测试的问题。所以mock object是unit test的一部分。   Mock的好处是">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"always","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: 'GRLHBBCD9E',
      apiKey: '759ab3f5c09b132b3ed4842a44c53c45',
      indexName: 'mycommons',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://mycommons.cn/2016/07/19/Mockito/"/>





  <title>Mockito浅谈 | 流水不腐小夏</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?55458ffca1f60dd283f11b9a7f82b262";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">流水不腐小夏</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">http://mycommons.cn</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  
  <div class="algolia-popup popup search-popup">
    <div class="algolia-search">
      <div class="algolia-search-input-icon">
        <i class="fa fa-search"></i>
      </div>
      <div class="algolia-search-input" id="algolia-search-input"></div>
    </div>

    <div class="algolia-results">
      <div id="algolia-stats"></div>
      <div id="algolia-hits"></div>
      <div id="algolia-pagination" class="algolia-pagination"></div>
    </div>

    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
  </div>




    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://mycommons.cn/2016/07/19/Mockito/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="流水不腐小夏">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="流水不腐小夏">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">Mockito浅谈</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-07-19T14:13:01+08:00">
                2016-07-19
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java测试/" itemprop="url" rel="index">
                    <span itemprop="name">Java测试</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2016/07/19/Mockito/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2016/07/19/Mockito/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv">本文总阅读量
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>次
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="Mockito浅谈"><a href="#Mockito浅谈" class="headerlink" title="Mockito浅谈"></a>Mockito浅谈</h1><hr>
<p>[TOC]</p>
<h1 id="Mockito简介"><a href="#Mockito简介" class="headerlink" title="Mockito简介"></a>Mockito简介</h1><ul>
<li><p>什么是mock？<br>在软件开发的世界之外, “mock”一词是指模仿或者效仿。 因此可以将“mock”理解为一个替身，替代者. 在软件开发中提及”mock”，通常理解为模拟对象或者Fake。</p>
</li>
<li><p>为什么需要Mock?<br>Mock是为了解决units之间由于耦合而难于被测试的问题。所以mock object是unit test的一部分。</p>
</li>
</ul>
<h1 id="Mock的好处是什么"><a href="#Mock的好处是什么" class="headerlink" title="Mock的好处是什么?"></a>Mock的好处是什么?</h1><ul>
<li><p>提前创建测试，TDD（测试驱动开发） </p>
<blockquote>
<p>这是个最大的好处吧。如果你创建了一个Mock那么你就可以在service接口创建之前写Service Tests了，这样你就能在开发过程中把测试添加到你的自动化测试环境中了。换句话说，模拟使你能够使用测试驱动开发。 </p>
</blockquote>
</li>
<li><p>团队可以并行工作 </p>
<blockquote>
<p>这类似于上面的那点；为不存在的代码创建测试。但前面讲的是开发人员编写测试程序，这里说的是测试团队来创建。当还没有任何东西要测的时候测试团队如何来创建测试呢？模拟并针对模拟测试！这意味着当service借口需要测试时，实际上QA团队已经有了一套完整的测试组件；没有出现一个团队等待另一个团队完成的情况。这使得模拟的效益型尤为突出了。</p>
</blockquote>
</li>
<li><p>你可以创建一个验证或者演示程序。 </p>
<blockquote>
<p>由于Mocks非常高效，Mocks可以用来创建一个概念证明，作为一个示意图，或者作为一个你正考虑构建项目的演示程序。这为你决定项目接下来是否要进行提供了有力的基础，但最重要的还是提供了实际的设计决策。 </p>
</blockquote>
</li>
<li><p>为无法访问的资源编写测试</p>
<blockquote>
<p>这个好处不属于实际效益的一种，而是作为一个必要时的“救生圈”。有没有遇到这样的情况？当你想要测试一个service接口，但service需要经过防火墙访问，防火墙不能为你打开或者你需要认证才能访问。遇到这样情况时，你可以在你能访问的地方使用MockService替代，这就是一个“救生圈”功能。 </p>
</blockquote>
</li>
<li><p>Mock 可以交给用户 </p>
<blockquote>
<p>在有些情况下，某种原因你需要允许一些外部来源访问你的测试系统，像合作伙伴或者客户。这些原因导致别人也可以访问你的敏感信息，而你或许只是想允许访问部分测试环境。在这种情况下，如何向合作伙伴或者客户提供一个测试系统来开发或者做测试呢？最简单的就是提供一个mock，无论是来自于你的网络或者客户的网络。soapUI mock非常容易配置，他可以运行在soapUI或者作为一个war包发布到你的java服务器里面。</p>
</blockquote>
</li>
<li><p>隔离系统 </p>
<blockquote>
<p>有时，你希望在没有系统其他部分的影响下测试系统单独的一部分。由于其他系统部分会给测试数据造成干扰，影响根据数据收集得到的测试结论。使用mock你可以移除掉除了需要测试部分的系统依赖的模拟。当隔离这些mocks后，mocks就变得非常简单可靠，快速可预见。这为你提供了一个移除了随机行为，有重复模式并且可以监控特殊系统的测试环境。 </p>
</blockquote>
</li>
</ul>
<h1 id="Mockito使用示例"><a href="#Mockito使用示例" class="headerlink" title="Mockito使用示例"></a>Mockito使用示例</h1><h2 id="模拟对象"><a href="#模拟对象" class="headerlink" title="模拟对象"></a>模拟对象</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// 模拟LinkedList 的一个对象  </span></div><div class="line">LinkedList mockedList = mock(LinkedList.class);</div><div class="line"><span class="comment">// 此时调用get方法，会返回null，因为还没有对方法调用的返回值做模拟</span></div><div class="line">System.out.println(mockedList.get(<span class="number">0</span>));</div></pre></td></tr></table></figure>
<h2 id="模拟方法调用的返回值"><a href="#模拟方法调用的返回值" class="headerlink" title="模拟方法调用的返回值"></a>模拟方法调用的返回值</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// 模拟获取第一个元素时，返回字符串first。  给特定的方法调用返回固定值在官方说法中称为stub。</span></div><div class="line">when(mockedList.get(<span class="number">0</span>)).thenReturn(<span class="string">"first"</span>);</div><div class="line"><span class="comment">// 此时打印输出first</span></div><div class="line">System.out.println(mockedList.get(<span class="number">0</span>));</div></pre></td></tr></table></figure>
<h2 id="模拟方法调用抛出异常"><a href="#模拟方法调用抛出异常" class="headerlink" title="模拟方法调用抛出异常"></a>模拟方法调用抛出异常</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// 模拟获取第二个元素时，抛出RuntimeException  </span></div><div class="line">when(mockedList.get(<span class="number">1</span>)).thenThrow(<span class="keyword">new</span> RuntimeException());</div><div class="line"><span class="comment">// 此时将会抛出RuntimeException  </span></div><div class="line">System.out.println(mockedList.get(<span class="number">1</span>));</div></pre></td></tr></table></figure>
<p>如果一个函数没有返回值类型，那么可以使用此方法模拟异常抛出<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">doThrow(<span class="keyword">new</span> RuntimeException(<span class="string">"clear exception"</span>)).when(mockedList).clear();</div><div class="line">mockedList.clear();</div></pre></td></tr></table></figure></p>
<h2 id="模拟调用方法时的参数匹配"><a href="#模拟调用方法时的参数匹配" class="headerlink" title="模拟调用方法时的参数匹配"></a>模拟调用方法时的参数匹配</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// anyInt()匹配任何int参数，这意味着参数为任意值，其返回值均是element  </span></div><div class="line">when(mockedList.get(anyInt())).thenReturn(<span class="string">"element"</span>);</div><div class="line"><span class="comment">// 此时打印是element</span></div><div class="line">System.out.println(mockedList.get(<span class="number">999</span>));</div></pre></td></tr></table></figure>
<h2 id="模拟方法调用次数"><a href="#模拟方法调用次数" class="headerlink" title="模拟方法调用次数"></a>模拟方法调用次数</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// 调用add一次</span></div><div class="line">mockedList.add(<span class="string">"once"</span>); </div><div class="line"><span class="comment">// 下面两个写法验证效果一样，均验证add方法是否被调用了一次</span></div><div class="line">verify(mockedList).add(<span class="string">"once"</span>);</div><div class="line">verify(mockedList, times(<span class="number">1</span>)).add(<span class="string">"once"</span>);</div></pre></td></tr></table></figure>
<h2 id="校验行为"><a href="#校验行为" class="headerlink" title="校验行为"></a>校验行为</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// mock creation</span></div><div class="line">List mockedList = mock(List.class);</div><div class="line"><span class="comment">// using mock object</span></div><div class="line">mockedList.add(<span class="string">"one"</span>);</div><div class="line">mockedList.clear();</div><div class="line"><span class="comment">//verification</span></div><div class="line">verify(mockedList).add(<span class="string">"one"</span>);</div><div class="line">verify(mockedList).clear();</div></pre></td></tr></table></figure>
<h2 id="模拟方法调用-Stubbing"><a href="#模拟方法调用-Stubbing" class="headerlink" title="模拟方法调用(Stubbing)"></a>模拟方法调用(Stubbing)</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//You can mock concrete classes, not just interfaces</span></div><div class="line">LinkedList mockedList = mock(LinkedList.class);</div><div class="line"><span class="comment">//stubbing</span></div><div class="line">when(mockedList.get(<span class="number">0</span>)).thenReturn(<span class="string">"first"</span>);</div><div class="line">when(mockedList.get(<span class="number">1</span>)).thenThrow(<span class="keyword">new</span> RuntimeException());</div><div class="line"><span class="comment">//following prints "first"</span></div><div class="line">System.out.println(mockedList.get(<span class="number">0</span>));</div><div class="line"><span class="comment">//following throws runtime exception</span></div><div class="line">System.out.println(mockedList.get(<span class="number">1</span>));</div><div class="line"><span class="comment">//following prints "null" because get(999) was not stubbed</span></div><div class="line">System.out.println(mockedList.get(<span class="number">999</span>));</div><div class="line"></div><div class="line">verify(mockedList).get(<span class="number">0</span>);</div></pre></td></tr></table></figure>
<h2 id="参数匹配"><a href="#参数匹配" class="headerlink" title="参数匹配"></a>参数匹配</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//stubbing using built-in anyInt() argument matcher</span></div><div class="line">when(mockedList.get(anyInt())).thenReturn(<span class="string">"element"</span>);</div><div class="line"><span class="comment">//stubbing using custom matcher (let's say isValid() returns your own matcher implementation):</span></div><div class="line">when(mockedList.contains(argThat(isValid()))).thenReturn(<span class="string">"element"</span>);</div><div class="line"><span class="comment">//following prints "element"</span></div><div class="line">System.out.println(mockedList.get(<span class="number">999</span>));</div><div class="line"><span class="comment">//you can also verify using an argument matcher</span></div><div class="line">verify(mockedList).get(anyInt());</div><div class="line"><span class="comment">//argument matchers can also be written as Java 8 Lambdas</span></div><div class="line">verify(mockedList).add(someString -&gt; someString.length() &gt; <span class="number">5</span>);</div></pre></td></tr></table></figure>
<h2 id="校验方法调用次数"><a href="#校验方法调用次数" class="headerlink" title="校验方法调用次数"></a>校验方法调用次数</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//using mock</span></div><div class="line">mockedList.add(<span class="string">"once"</span>);</div><div class="line"></div><div class="line">mockedList.add(<span class="string">"twice"</span>);</div><div class="line">mockedList.add(<span class="string">"twice"</span>);</div><div class="line"></div><div class="line">mockedList.add(<span class="string">"three times"</span>);</div><div class="line">mockedList.add(<span class="string">"three times"</span>);</div><div class="line">mockedList.add(<span class="string">"three times"</span>);</div><div class="line"><span class="comment">//following two verifications work exactly the same - times(1) is used by default</span></div><div class="line">verify(mockedList).add(<span class="string">"once"</span>);</div><div class="line">verify(mockedList, times(<span class="number">1</span>)).add(<span class="string">"once"</span>);</div><div class="line"><span class="comment">//exact number of invocations verification</span></div><div class="line">verify(mockedList, times(<span class="number">2</span>)).add(<span class="string">"twice"</span>);</div><div class="line">verify(mockedList, times(<span class="number">3</span>)).add(<span class="string">"three times"</span>);</div><div class="line"><span class="comment">//verification using never(). never() is an alias to times(0)</span></div><div class="line">verify(mockedList, never()).add(<span class="string">"never happened"</span>);</div><div class="line"><span class="comment">//verification using atLeast()/atMost()</span></div><div class="line">verify(mockedList, atLeastOnce()).add(<span class="string">"three times"</span>);</div><div class="line">verify(mockedList, atLeast(<span class="number">2</span>)).add(<span class="string">"five times"</span>);</div><div class="line">verify(mockedList, atMost(<span class="number">5</span>)).add(<span class="string">"three times"</span>);</div></pre></td></tr></table></figure>
<h2 id="模拟无返回方法抛出异常"><a href="#模拟无返回方法抛出异常" class="headerlink" title="模拟无返回方法抛出异常"></a>模拟无返回方法抛出异常</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">doThrow(<span class="keyword">new</span> RuntimeException()).when(mockedList).clear();</div><div class="line"><span class="comment">//following throws RuntimeException:</span></div><div class="line">mockedList.clear();</div></pre></td></tr></table></figure>
<h2 id="校验方法调用顺序"><a href="#校验方法调用顺序" class="headerlink" title="校验方法调用顺序"></a>校验方法调用顺序</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// A. Single mock whose methods must be invoked in a particular order</span></div><div class="line">List singleMock = mock(List.class);</div><div class="line"><span class="comment">//using a single mock</span></div><div class="line">singleMock.add(<span class="string">"was added first"</span>);</div><div class="line">singleMock.add(<span class="string">"was added second"</span>);</div><div class="line"><span class="comment">//create an inOrder verifier for a single mock</span></div><div class="line">InOrder inOrder = inOrder(singleMock);</div><div class="line"><span class="comment">//following will make sure that add is first called with "was added first, then with "was added second"</span></div><div class="line">inOrder.verify(singleMock).add(<span class="string">"was added first"</span>);</div><div class="line">inOrder.verify(singleMock).add(<span class="string">"was added second"</span>);</div><div class="line"></div><div class="line"><span class="comment">// B. Multiple mocks that must be used in a particular order</span></div><div class="line">List firstMock = mock(List.class);</div><div class="line">List secondMock = mock(List.class);</div><div class="line"><span class="comment">//using mocks</span></div><div class="line">firstMock.add(<span class="string">"was called first"</span>);</div><div class="line">secondMock.add(<span class="string">"was called second"</span>);</div><div class="line"><span class="comment">//create inOrder object passing any mocks that need to be verified in order</span></div><div class="line">InOrder inOrder = inOrder(firstMock, secondMock);</div><div class="line"><span class="comment">//following will make sure that firstMock was called before secondMock</span></div><div class="line">inOrder.verify(firstMock).add(<span class="string">"was called first"</span>);</div><div class="line">inOrder.verify(secondMock).add(<span class="string">"was called second"</span>);</div><div class="line"><span class="comment">// Oh, and A + B can be mixed together at will</span></div></pre></td></tr></table></figure>
<h2 id="校验方法是否从未调用"><a href="#校验方法是否从未调用" class="headerlink" title="校验方法是否从未调用"></a>校验方法是否从未调用</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//using mocks - only mockOne is interacted</span></div><div class="line">mockOne.add(<span class="string">"one"</span>);</div><div class="line"><span class="comment">//ordinary verification</span></div><div class="line">verify(mockOne).add(<span class="string">"one"</span>);</div><div class="line"><span class="comment">//verify that method was never called on a mock</span></div><div class="line">verify(mockOne, never()).add(<span class="string">"two"</span>);</div><div class="line"><span class="comment">//verify that other mocks were not interacted</span></div><div class="line">verifyZeroInteractions(mockTwo, mockThree);</div></pre></td></tr></table></figure>
<h2 id="快速创建Mock对象"><a href="#快速创建Mock对象" class="headerlink" title="快速创建Mock对象"></a>快速创建Mock对象</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ArticleManagerTest</span> </span>&#123;</div><div class="line">   <span class="meta">@Mock</span> <span class="keyword">private</span> ArticleCalculator calculator;</div><div class="line">      <span class="meta">@Mock</span> <span class="keyword">private</span> ArticleDatabase database;</div><div class="line">      <span class="meta">@Mock</span> <span class="keyword">private</span> UserProvider userProvider;</div><div class="line">      <span class="meta">@Before</span></div><div class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">before</span><span class="params">()</span></span>&#123;</div><div class="line">          MockitoAnnotations.initMocks(<span class="keyword">this</span>);</div><div class="line">      &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="自定义返回不同结果"><a href="#自定义返回不同结果" class="headerlink" title="自定义返回不同结果"></a>自定义返回不同结果</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">when(mock.someMethod(<span class="string">"some arg"</span>))</div><div class="line">   .thenThrow(<span class="keyword">new</span> RuntimeException())  <span class="comment">// 第一次会抛出异常</span></div><div class="line">   .thenReturn(<span class="string">"foo"</span>); <span class="comment">// 第二次会返回这个结果</span></div><div class="line"><span class="comment">//First call: throws runtime exception:</span></div><div class="line">mock.someMethod(<span class="string">"some arg"</span>); <span class="comment">// 第一次</span></div><div class="line"><span class="comment">//Second call: prints "foo"</span></div><div class="line">System.out.println(mock.someMethod(<span class="string">"some arg"</span>)); <span class="comment">// 第二次</span></div><div class="line"><span class="comment">//Any consecutive call: prints "foo" as well (last stubbing wins).</span></div><div class="line">System.out.println(mock.someMethod(<span class="string">"some arg"</span>)); <span class="comment">// 第n次(n&gt; 2)，依旧以最后返回最后一个配置</span></div></pre></td></tr></table></figure>
<h2 id="对返回结果进行拦截"><a href="#对返回结果进行拦截" class="headerlink" title="对返回结果进行拦截"></a>对返回结果进行拦截</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">when(mock.someMethod(anyString())).thenAnswer(<span class="keyword">new</span> Answer() &#123;</div><div class="line">    <span class="function">Object <span class="title">answer</span><span class="params">(InvocationOnMock invocation)</span> </span>&#123;</div><div class="line">        Object[] args = invocation.getArguments();</div><div class="line">        Object mock = invocation.getMock();</div><div class="line">        <span class="keyword">return</span> <span class="string">"called with arguments: "</span> + args;</div><div class="line">    &#125;</div><div class="line">&#125;);</div><div class="line"><span class="comment">//the following prints "called with arguments: foo"</span></div><div class="line">System.out.println(mock.someMethod(<span class="string">"foo"</span>));</div></pre></td></tr></table></figure>
<h2 id="Mock函数操作"><a href="#Mock函数操作" class="headerlink" title="Mock函数操作"></a>Mock函数操作</h2><p>可以通过doThrow(), doAnswer(), doNothing(), doReturn() and doCallRealMethod() 来自定义函数操作。</p>
<h2 id="暗中调用真实对象"><a href="#暗中调用真实对象" class="headerlink" title="暗中调用真实对象"></a>暗中调用真实对象</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">List list = <span class="keyword">new</span> LinkedList();</div><div class="line">List spy = spy(list);</div><div class="line"><span class="comment">//optionally, you can stub out some methods:</span></div><div class="line">when(spy.size()).thenReturn(<span class="number">100</span>);</div><div class="line"><span class="comment">//using the spy calls *real* methods</span></div><div class="line">spy.add(<span class="string">"one"</span>);</div><div class="line">spy.add(<span class="string">"two"</span>);</div><div class="line"><span class="comment">//prints "one" - the first element of a list</span></div><div class="line">System.out.println(spy.get(<span class="number">0</span>));</div><div class="line"><span class="comment">//size() method was stubbed - 100 is printed</span></div><div class="line">System.out.println(spy.size());</div><div class="line"><span class="comment">//optionally, you can verify</span></div><div class="line">verify(spy).add(<span class="string">"one"</span>);</div><div class="line">   verify(spy).add(<span class="string">"two"</span>);</div></pre></td></tr></table></figure>
<h2 id="改变默认返回值"><a href="#改变默认返回值" class="headerlink" title="改变默认返回值"></a>改变默认返回值</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">Foo mock = mock(Foo.class, Mockito.RETURNS_SMART_NULLS);</div><div class="line">Foo mockTwo = mock(Foo.class, <span class="keyword">new</span> YourOwnAnswer());</div></pre></td></tr></table></figure>
<h2 id="捕获函数的参数值"><a href="#捕获函数的参数值" class="headerlink" title="捕获函数的参数值"></a>捕获函数的参数值</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">ArgumentCaptor&lt;Person&gt; argument = ArgumentCaptor.forClass(Person.class);</div><div class="line">verify(mock).doSomething(argument.capture());</div><div class="line">assertEquals(<span class="string">"John"</span>, argument.getValue().getName());</div></pre></td></tr></table></figure>
<h2 id="部分Mock"><a href="#部分Mock" class="headerlink" title="部分Mock"></a>部分Mock</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//you can create partial mock with spy() method:</span></div><div class="line">List list = spy(<span class="keyword">new</span> LinkedList());</div><div class="line"><span class="comment">//you can enable partial mock capabilities selectively on mocks:</span></div><div class="line">Foo mock = mock(Foo.class);</div><div class="line"><span class="comment">//Be sure the real implementation is 'safe'.</span></div><div class="line"><span class="comment">//If real implementation throws exceptions or depends on specific state of the object then you're in trouble.</span></div><div class="line">when(mock.someMethod()).thenCallRealMethod();</div></pre></td></tr></table></figure>
<h2 id="重置Mock"><a href="#重置Mock" class="headerlink" title="重置Mock"></a>重置Mock</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">List mock = mock(List.class);</div><div class="line">when(mock.size()).thenReturn(<span class="number">10</span>);</div><div class="line">mock.add(<span class="number">1</span>);</div><div class="line">reset(mock);</div><div class="line"><span class="comment">//at this point the mock forgot any interactions &amp; stubbing</span></div></pre></td></tr></table></figure>
<h2 id="序列化"><a href="#序列化" class="headerlink" title="序列化"></a>序列化</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">List&lt;Object&gt; list = <span class="keyword">new</span> ArrayList&lt;Object&gt;();</div><div class="line">List&lt;Object&gt; spy = mock(ArrayList.class, withSettings()</div><div class="line">                 .spiedInstance(list)</div><div class="line">                 .defaultAnswer(CALLS_REAL_METHODS)</div><div class="line">                 .serializable());</div></pre></td></tr></table></figure>
<h2 id="检查超时"><a href="#检查超时" class="headerlink" title="检查超时"></a>检查超时</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//passes when someMethod() is called within given time span</span></div><div class="line">verify(mock, timeout(<span class="number">100</span>)).someMethod();</div><div class="line"><span class="comment">//above is an alias to:</span></div><div class="line">verify(mock, timeout(<span class="number">100</span>).times(<span class="number">1</span>)).someMethod();</div><div class="line"><span class="comment">//passes when som`eMethod() is called *exactly* 2 times within given time span</span></div><div class="line">verify(mock, timeout(<span class="number">100</span>).times(<span class="number">2</span>)).someMethod();</div><div class="line"><span class="comment">//passes when someMethod() is called *at least* 2 times within given time span</span></div><div class="line">verify(mock, timeout(<span class="number">100</span>).atLeast(<span class="number">2</span>)).someMethod();</div><div class="line"><span class="comment">//verifies someMethod() within given time span using given verification mode</span></div><div class="line"><span class="comment">//useful only if you have your own custom verification modes.</span></div><div class="line">verify(mock, <span class="keyword">new</span> Timeout(<span class="number">100</span>, yourOwnVerificationMode)).someMethod();</div></pre></td></tr></table></figure>
<h2 id="Mock详情"><a href="#Mock详情" class="headerlink" title="Mock详情"></a>Mock详情</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">Mockito.mockingDetails(someObject).isMock();</div><div class="line">Mockito.mockingDetails(someObject).isSpy();</div></pre></td></tr></table></figure>
      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechat_pay.jpeg" alt="流水不腐小夏 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpeg" alt="流水不腐小夏 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/测试/" rel="tag"># 测试</a>
          
            <a href="/tags/Java测试/" rel="tag"># Java测试</a>
          
            <a href="/tags/Mockito/" rel="tag"># Mockito</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/07/19/JUnit/" rel="next" title="JUnit浅谈">
                <i class="fa fa-chevron-left"></i> JUnit浅谈
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/07/19/Android-Studio-Refactor/" rel="prev" title="使用Android Studio对代码进行重构">
                使用Android Studio对代码进行重构 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.jpeg"
                alt="流水不腐小夏" />
            
              <p class="site-author-name" itemprop="name">流水不腐小夏</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">55</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/LiushuiXiaoxia" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-GitHub"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://www.jianshu.com/u/58771f980bac" target="_blank" title="简书">
                    
                      <i class="fa fa-fw fa-简书"></i>简书</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://blog.csdn.net/guijiaoba" target="_blank" title="CSDN">
                    
                      <i class="fa fa-fw fa-CSDN"></i>CSDN</a>
                </span>
              
            
          </div>

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Mockito浅谈"><span class="nav-number">1.</span> <span class="nav-text">Mockito浅谈</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Mockito简介"><span class="nav-number">2.</span> <span class="nav-text">Mockito简介</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Mock的好处是什么"><span class="nav-number">3.</span> <span class="nav-text">Mock的好处是什么?</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Mockito使用示例"><span class="nav-number">4.</span> <span class="nav-text">Mockito使用示例</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#模拟对象"><span class="nav-number">4.1.</span> <span class="nav-text">模拟对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#模拟方法调用的返回值"><span class="nav-number">4.2.</span> <span class="nav-text">模拟方法调用的返回值</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#模拟方法调用抛出异常"><span class="nav-number">4.3.</span> <span class="nav-text">模拟方法调用抛出异常</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#模拟调用方法时的参数匹配"><span class="nav-number">4.4.</span> <span class="nav-text">模拟调用方法时的参数匹配</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#模拟方法调用次数"><span class="nav-number">4.5.</span> <span class="nav-text">模拟方法调用次数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#校验行为"><span class="nav-number">4.6.</span> <span class="nav-text">校验行为</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#模拟方法调用-Stubbing"><span class="nav-number">4.7.</span> <span class="nav-text">模拟方法调用(Stubbing)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#参数匹配"><span class="nav-number">4.8.</span> <span class="nav-text">参数匹配</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#校验方法调用次数"><span class="nav-number">4.9.</span> <span class="nav-text">校验方法调用次数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#模拟无返回方法抛出异常"><span class="nav-number">4.10.</span> <span class="nav-text">模拟无返回方法抛出异常</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#校验方法调用顺序"><span class="nav-number">4.11.</span> <span class="nav-text">校验方法调用顺序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#校验方法是否从未调用"><span class="nav-number">4.12.</span> <span class="nav-text">校验方法是否从未调用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#快速创建Mock对象"><span class="nav-number">4.13.</span> <span class="nav-text">快速创建Mock对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#自定义返回不同结果"><span class="nav-number">4.14.</span> <span class="nav-text">自定义返回不同结果</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#对返回结果进行拦截"><span class="nav-number">4.15.</span> <span class="nav-text">对返回结果进行拦截</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Mock函数操作"><span class="nav-number">4.16.</span> <span class="nav-text">Mock函数操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#暗中调用真实对象"><span class="nav-number">4.17.</span> <span class="nav-text">暗中调用真实对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#改变默认返回值"><span class="nav-number">4.18.</span> <span class="nav-text">改变默认返回值</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#捕获函数的参数值"><span class="nav-number">4.19.</span> <span class="nav-text">捕获函数的参数值</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#部分Mock"><span class="nav-number">4.20.</span> <span class="nav-text">部分Mock</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#重置Mock"><span class="nav-number">4.21.</span> <span class="nav-text">重置Mock</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#序列化"><span class="nav-number">4.22.</span> <span class="nav-text">序列化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#检查超时"><span class="nav-number">4.23.</span> <span class="nav-text">检查超时</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Mock详情"><span class="nav-number">4.24.</span> <span class="nav-text">Mock详情</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2015 &mdash; <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">流水不腐小夏</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.3</div>




        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  

  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  

    
      <script id="dsq-count-scr" src="https://mycommons-cn.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'http://mycommons.cn/2016/07/19/Mockito/';
          this.page.identifier = '2016/07/19/Mockito/';
          this.page.title = 'Mockito浅谈';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://mycommons-cn.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  




  
  
  
  <link rel="stylesheet" href="/lib/algolia-instant-search/instantsearch.min.css">

  
  
  <script src="/lib/algolia-instant-search/instantsearch.min.js"></script>
  

  <script src="/js/src/algolia-search.js?v=5.1.3"></script>



  

  

  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
